const {resolve} = require('path');
const WebpackHtmlPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');

// 默认打包采用的时生产环境
// process.env.NODE_ENV = 'development';

module.exports = {
  entry: './src/index.js',
  output:{
    filename: 'built.js',
    path: resolve(__dirname, 'build')
  },
  module:{
    rules: [
      {
        test: /\.css$/,
        use: [
          // 'style-loader',
          // 取代style-loader, 提取js中的css单独成文件
          MiniCssExtractPlugin.loader,
          'css-loader', // 这是默认配置
          // css兼容性处理postcss-loader, postcss-preset-env
          {
            // postcss-loader postcss-preset-env 帮postcss找到package.json中browserslist里面的配置，加载指定css兼容样式
            /*
            "browserslist": {
              "development": [
                "last 1 chrome version",
                "last 1 firefox version",
                "last 1 safari version"
              ],
              "production": [
                ">0.2%",
                "not dead",
                "not op_mini all"
              ]
            }
            */
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  ['postcss-preset-env',{}]
                ]
              }
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new WebpackHtmlPlugin({
      template: './public/index.html',
      inject: "body"
    }),
    new MiniCssExtractPlugin({
      filename: './css/main.css'
    }),
    new OptimizeCssAssetsWebpackPlugin()
  ],
  mode: 'development'
}